<template>
	<view class="uni-q00_home">
		<view class="home_search__container">
			<my-search
			@searchHandler="gotoSearch"
		></my-search>
		</view>
		
		<swiper class="home_swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, index) in swiperList" :key="index">
				<navigator class="swiper-item" :url="'/subpkg/goods_details/goods_details?goods_id=' + item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<view class="home_nav">
			<view class="nav_item" v-for="(item, index) in navList" :key="index" @click="navClikHandle(item)">
				<image class="nav_img" :src="item.image_src" ></image>
			</view>
		</view>
		<view class="home_floor">
			<view class="floor_item" v-for="(item, index) in goodsList" :key="index">
				<view class="floor_title">
					<image :src="item.floor_title.image_src" mode=""></image>
				</view>
				<view class="floor_contain">
					<view class="floor_contain__left">
						<navigator :url="item.product_list[0].url">
							<image :src="item.product_list[0].image_src" :style="{ width: item.product_list[0].image_width + 'rpx'}"  mode="widthFix"></image>
						</navigator>
					</view>
					<view class="floor_contain__right">
						<navigator v-for="(item2, index) in item.product_list" :key="index" :url="item2.url">
							<image v-if="index !== 0" :src="item2.image_src" :style="{ width: item2.image_width + 'rpx'}"  mode="widthFix"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { homeApi } from '../../static/api'
import { showMsg } from '../../static/common'
import badgeMix from '@/mixins/tabbar-badge'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				swiperList: [],//轮播图
				navList: [],	//分类导航数据列表
				goodsList: [],
			};
		},
		onLoad() {
			this.getSwiperList();
			this.getNavList();
			this.getFloorList();
		},
		methods: {
			async getSwiperList() {
				 await homeApi.swiperList({}).then(({data}) => {
					 if( data.meta.status !== 200) {
					 	return showMsg.showErrMsg();
					 } 
					 this.swiperList = data.message;	
				 })
				 .catch(err => {
					 console.log(err);
				 });
			},
			
			async getNavList() {
				await homeApi.navList()
				.then(({ data}) => {
					if(data.meta.status !== 200) {
						return showMsg.showErrMsg();
					}
					this.navList = data.message;
				})
				.catch(err => {
					console.log(meta.msg);
				})
			},
			
			async getFloorList() {
				await homeApi.floorList()
				.then(({ data }) => {
					if( data.meta.status !== 200) return showMsg.showErrMsg('商品数据请求失败！');
					this.floorNavUrlHandle(data.message);
					this.goodsList = data.message;
					console.log(this.goodsList);
				})
				.catch(err => {
					console.log(data.meta.msg);
				});
			},
			
			floorNavUrlHandle(floorData) {
				floorData.forEach(( floor ) => {
					floor.product_list.map(( prod ) => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1];
					});
				});
			},
			navClikHandle( navList ) {
				if( navList.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate',
					})
				}
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/cate-search/cate-search',
				})
			},
		},
	}
</script>

<style lang="scss">
.uni-q00_home {
	.home_swiper {
		height: 330rpx;
		.swiper-item,
		image {
			height: 100%;
			width: 100%;
		}
	}
	.home_nav {
		display: flex;
		justify-content: space-around;
		margin: 18rpx 0;
		.nav_img {
			width: 128rpx;
			height: 140rpx;
		}
	}
	.home_floor {
		margin-top: 50rpx;
		padding-left: 10rpx;
		.floor_item {
			.floor_title image {
				height: 60rpx;
				width: 100%;
			}
			.floor_contain {
				display: flex;
				justify-content: space-around;
				.floor_contain__right {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}
	}
	.home_search__container {
		position: sticky;
		top: 0;
		z-index: 99999999;
	}
}
</style>
